package com.webmall.servlet.cart;

import com.webmall.entity.CartEntity;
import com.webmall.entity.ProdEntity;
import com.webmall.entity.UserEntity;
import com.webmall.service.webmall_CartDao;
import com.webmall.service.webmall_ProdDao;
import com.webmall.servlet.user.UserLogin;

import javax.persistence.criteria.CriteriaBuilder;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/CartAdd")
public class CartAdd extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        //1--获取参数
        String pid = request.getParameter("id");
        String count = request.getParameter("count");
        String option = request.getParameter("option");

        //2--判断用户是否登录
        HttpSession session = request.getSession();
        String isLogin = String.valueOf(session.getAttribute("isLogin"));


        if (!isLogin.equals("1")){
            PrintWriter out = response.getWriter();
            out.write("<script>");
            out.write("alert('请先登录！');");
            out.write("location.href='login.jsp';");
            out.write("</script>");
            out.close();
        }else {

            UserEntity user = (UserEntity) session.getAttribute("login_user");
            String uid = user.getUSER_ID();

            //**查询有咩有这个商品的记录
            CartEntity cart_have = webmall_CartDao.selectById(uid,pid);
            if (cart_have != null){
                int have_count = cart_have.getCART_MOUNT();
                have_count +=Integer.parseInt(count);//新增
                if (have_count>=5){
                    have_count =5;
                }
                webmall_CartDao.updateCart(cart_have.getCART_ID(),have_count);
            }else {

                ProdEntity prod = (ProdEntity) webmall_ProdDao.selectByCATEId(2, pid).get(0);
                CartEntity cart = new CartEntity(
                        0, prod.getPRODUCT_FILENAME(),
                        prod.getPRODUCT_NAME(), String.valueOf(prod.getPRODUCT_PRICE()), Integer.valueOf(count),
                        prod.getPRODUCT_STOCK(), Integer.valueOf(pid), uid, 1
                );
                int count_op = webmall_CartDao.insertCart(cart);
                request.setAttribute("prod",prod);
            }

           // request.getRequestDispatcher();

            if (option.equals("buy")){//立即购买
                request.getRequestDispatcher("/ShowCart").forward(request,response);
            }else {
                response.sendRedirect("/selectProDetail?id="+pid);
            }
        }

    }
}
